package com.roc.web.action.yycard;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.TblMchtInf;
import com.roc.pojo.TblSettleItem;
import com.roc.service.operator.IPowerInfoService;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;
import com.roc.web.action.settlemgr.SettlementApplyMoneySearchCondition;
@Component
@Scope("prototype")
@Namespace("/card")
public class ScanCodeAction extends PaginationBaseAction{
	
	private static final long serialVersionUID = 1L;
	private ScanCodeActionSearchCondition searchCondition;
	private static Logger logger = LogManager.getLogger(ScanCodeAction.class);
	@Autowired
	private IPageService pagenitionService;
	@Autowired
	private IPowerInfoService powerInfo;
	/**
	 * 初始化页面
	 * @return
	 * @throws Exception
	 */
	@Action(value = "init", results = { @Result(name = "success", location = "/pages/yycard/scanCodeList.jsp")
		,@Result(name="error",location="/error.jsp")})
	public String initMerinfoPage(){
		try{
			Operators op=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
			if(op.getRoleType().equals(Role.SYSTEM.getCode())||
					op.getRoleType().equals(Role.O_MANAGER.getCode())||
					op.getRoleType().equals(Role.AGENT.getCode())||
					op.getRoleType().equals(Role.OPERATORS_COMM.getCode())){
				if(op.getRoleType().equals(Role.OPERATORS_COMM.getCode())){//运营先行判断
					String roleType=powerInfo.querySuperRoleByOpId(op.getSuperiorId());
					if(roleType.equals(Role.SYSTEM.getCode())||
						roleType.equals(Role.O_MANAGER.getCode())){
						
					}else{
						addActionMessage("无权限访问");
						return ERROR;
					}
				}
				logger.info("页面初始化");
				return SUCCESS;
			}			
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionError(e.getMessage());
			return ERROR;
		}		
		addActionMessage("当前登录人员无权限访问!");
		return ERROR;
	}
	
	@Action(value="queryMerinfo",results={@Result(name="success",location="/pages/yycard/scanCodeList.jsp")
	,@Result(name="error",location="/error.jsp")})
	public String querySettlementMsgList(){
		try {
			Operators op = (Operators) getSession().getAttribute(
					Constant.SESSION_LOGIN_MERCHANT);
			String roleTypeRtn = op.getRoleType();
			Long operId = op.getId();
			if(op.getRoleType().equals(Role.SYSTEM.getCode())||
					op.getRoleType().equals(Role.O_MANAGER.getCode())||
					op.getRoleType().equals(Role.AGENT.getCode())||
					op.getRoleType().equals(Role.OPERATORS_COMM.getCode())) {
				if(searchCondition == null){
					searchCondition = (ScanCodeActionSearchCondition) getSearchConditionFromSession();
					if(searchCondition == null){
						searchCondition = new ScanCodeActionSearchCondition();
					}
				}
				if(op.getRoleType().equals(Role.OPERATORS_COMM.getCode())){//运营先行判断
					String roleType=powerInfo.querySuperRoleByOpId(op.getSuperiorId());
					if(roleType.equals(Role.O_MANAGER.getCode())){
						searchCondition.setOpId(String.valueOf(op.getSuperiorId()));
						operId = op.getSuperiorId();
						roleTypeRtn = "02";
					}else if(roleType.equals(Role.SYSTEM.getCode())){
						roleTypeRtn = "00";
						operId = op.getSuperiorId();
					}else{
						}
				}else if(Role.O_MANAGER.getCode().equals(op.getRoleType()) || Role.AGENT.getCode().equals(op.getRoleType())){
					searchCondition.setOpId(String.valueOf(op.getId()));
				}else if(Role.SYSTEM.getCode().equals(op.getRoleType())){
				}else{
					addActionMessage("无权限访问");
					return ERROR;
				}
			int rows=pagenitionService.countSQL(searchCondition);
			if(rows>0){
				searchRsList=pagenitionService.findAllSQL(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
			}
			dealWithTheListSpecially(searchRsList);
			getRequest().setAttribute("searchRsList",searchRsList);
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			getRequest().setAttribute("oprId", operId);
			getRequest().setAttribute("oprRole", roleTypeRtn);
			setSearchToConditionSession(searchCondition);			
			return SUCCESS;
			}
			addActionError("无权限访问!!!");
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			addActionError("商户查询异常");
			return ERROR;
		}
		return ERROR;
	}
	
	
	@Override
	protected void dealWithTheListSpecially(List<Object> searchRsList) {
		List<TblMchtInf> tbl=new ArrayList<TblMchtInf>();
		if(searchRsList != null && searchRsList.size() > 0){
			Iterator<Object> it = searchRsList.iterator();
			while(it.hasNext()){
				Object[] obj = (Object[]) it.next();
				TblMchtInf tblMchtInf=new TblMchtInf();
				tblMchtInf.setId(Constance.Null2EmptyTrim(obj[0]));
				tblMchtInf.setName(Constance.Null2EmptyTrim(obj[1]));
				String zfb=Constance.Null2EmptyTrim(obj[2]);
				if("".equals(zfb)){
					zfb="2";
				}
				tblMchtInf.setExt1(zfb);
				String wx=Constance.Null2EmptyTrim(obj[3]);
				if("".equals(wx)){
					wx="00";
				}
				tblMchtInf.setExt2(wx);
				tblMchtInf.setBusiLiceNo(Constance.Null2EmptyTrim(obj[4]));
				tblMchtInf.setRegAddr(Constance.Null2EmptyTrim(obj[5]));
				tblMchtInf.setContactName(Constance.Null2EmptyTrim(obj[6]));
				tblMchtInf.setContactPhoneNo(Constance.Null2EmptyTrim(obj[7]));
				tblMchtInf.setExt3(Constance.Null2EmptyTrim(obj[8]));
				tblMchtInf.setExt4(Constance.Null2EmptyTrim(obj[9]));
				tblMchtInf.setMchtExtId(Constance.Null2EmptyTrim(obj[10]));
				tblMchtInf.setWxSubId(Constance.Null2EmptyTrim(obj[11]));
				tbl.add(tblMchtInf);
			}
		}
		searchRsList.clear();
		searchRsList.addAll(tbl);
	}

	public ScanCodeActionSearchCondition getSearchCondition() {
		return searchCondition;
	}

	public void setSearchCondition(ScanCodeActionSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		getSession().setAttribute(SEARCH_SESSION_SCAN_CODE_QUERY, searchCondition);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		return (ScanCodeActionSearchCondition)getSession().getAttribute(SEARCH_SESSION_SCAN_CODE_QUERY);
	}

	@Override
	protected String getInitPageSize() {
		
		return Constance.PAGE_MAX_SIZE+"";
	}

}
